//
//  AppDelegate.m
//  tuanGG
//
//  Created by 微他 on 15/9/16.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "AppDelegate.h"
#import "TabBarController.h"
#import "HomeViewController.h"
#import "KeyboardManager.h"
#import "UserInfo.h"
#import "LoginViewController.h"
#define App_key @"d13d56afc77a"
#define App_secrt @"72779cd321ba30d051d78cf4dcfc1f88"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    //设置启动页动画时间
    [NSThread sleepForTimeInterval:1.0];
    
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    //短信验证
    [SMSSDK registerApp:App_key withSecret:App_secrt];
    //一句话键盘弹出
    [[IQKeyboardManager sharedManager]setToolbarManageBehaviour:IQAutoToolbarByPosition];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    [[UserInfo sharedUserInfo] loadUserInfoFromSandbox];
    [self checklogin];
    NSLog(@"%@",NSHomeDirectory());
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checklogin) name:HANDLELOGIN object:nil];
    [[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    return YES;
}
-(void)checklogin{

    BOOL flag=[UserInfo sharedUserInfo].loginStatus;
    BOOL logo = [UserInfo sharedUserInfo].logo;
    NSLog(@"状态：%zi",flag);
    if (flag) {
        User_member *user_member = [User_member instance];
          [user_member loadfromDB:@{@"uid":[NSString stringWithFormat:@"\'%@\'",[[NSUserDefaults standardUserDefaults] objectForKey:@"supe_uid"]]}];
        TabBarController *homeVc=[[TabBarController alloc] init];
        _window.rootViewController=homeVc;
    }else{
        if (logo) {
            HomeViewController * leadVc = [[HomeViewController alloc]init];
            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:leadVc];
            [self.window setRootViewController:navc];

        }
        else{
            LoginViewController * leadVc = [[LoginViewController alloc]init];
            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:leadVc];
            [self.window setRootViewController:navc];
        }

        //        LoginViewController *vc = [[LoginViewController alloc] init];
        //        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        //        [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_03.png"]
        //                                forBarMetrics:UIBarMetricsDefault];
        //        [self.window setRootViewController:nav];
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark - 极光推送


//极光推送
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [LQJPushHelper registerDeviceToken:deviceToken];
    if ([APService registrationID]) {

        [self resetAliasAndTag];
    }
}

//怎么不走下面两个方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [LQJPushHelper handleRemoteNotification:userInfo completion:nil];
    //收到信息
    return;
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    //发送成功充值的通知

    //    [[NSNotificationCenter defaultCenter]postNotificationName:yyTuan_moneyIsYes object:nil];
    [LQJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
    // 应用正处理前台状态下，不会收到推送消息，因此在此处需要额外处理一下
    if (application.applicationState == UIApplicationStateActive) {
        //音效播放在里面
        //        [JKNotifier showNotifer:userInfo[@"aps"][@"alert"]];
    }

    return;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [LQJPushHelper showLocalNotificationAtFront:notification];
    return;
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Error : %@",error);
    
}
#pragma mark - 在这里设置别名，标签（程序开始的时候执行一次），后面从数据库获取就行了
- (void)resetAliasAndTag
{
    // 设置别名
    NSLog(@"%@",[User_member instance].pushkey);
    [APService setTags:nil alias:[User_member instance].pushkey callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
}
- (void)tagsAliasCallback:(int)iResCode
                     tags:(NSSet *)tags
                    alias:(NSString *)alias
{
    NSLog(@"alias     %@      %d",alias,iResCode);

    switch (iResCode) {
        case 6002:
            [self resetAliasAndTag];
            break;
        default:;
    }
}
@end
